(function($){
    $.fn.extend({
       validate: function(options){
                    // check date JavaScript function
                   // if date is valid then function returns true, otherwise returns false
                   function isDate(txtDate){
                       var objDate;  // date object initialized from the txtDate string
                       var mSeconds; // milliseconds from txtDate
                       // date length should be 10 characters - no more, no less
                       if (txtDate.length != 10) return false;
                       // extract day, month and year from the txtDate string
                       // expected format is mm/dd/yyyy
                       // subtraction will cast variables to integer implicitly
                            var day   = txtDate.substring(0,2)  - 0;
                            var month = txtDate.substring(3,5)  - 1; // because months in JS start with 0
                            var year  = txtDate.substring(6,10) - 0;
                        // third and sixth character should be /
                        if (txtDate.substring(2,3) != '-') return false;
                        if (txtDate.substring(5,6) != '-') return false;
                        // test year range
                        if (year < 999 || year > 3000) return false;
                        // convert txtDate to the milliseconds
                        mSeconds = (new Date(year, month, day)).getTime();
                        // initialize Date() object from calculated milliseconds
                        objDate = new Date();
                        objDate.setTime(mSeconds);
                        // compare input parameter date and created Date() object
                        // if difference exists then date isn't valid
                        if (objDate.getFullYear() != year)  return false;
                        if (objDate.getMonth()    != month) return false;
                        if (objDate.getDate()     != day)   return false;
                        // otherwise return true
                        return true;

                   }
               function isDateNormal(txtDate){
                       var objDate;  // date object initialized from the txtDate string
                       var mSeconds; // milliseconds from txtDate
                       // date length should be 10 characters - no more, no less
                       if (txtDate.length != 10) return false;
                       // extract day, month and year from the txtDate string
                       // expected format is mm/dd/yyyy
                       // subtraction will cast variables to integer implicitly
                            var day   = txtDate.substring(0,2)  - 0;
                            var month = txtDate.substring(3,5)  - 1; // because months in JS start with 0
                            var year  = txtDate.substring(6,10) - 0;
                        // third and sixth character should be /
                        if (txtDate.substring(2,3) != '/') return false;
                        if (txtDate.substring(5,6) != '/') return false;
                        // test year range
                        if (year < 999 || year > 3000) return false;
                        // convert txtDate to the milliseconds
                        mSeconds = (new Date(year, month, day)).getTime();
                        // initialize Date() object from calculated milliseconds
                        objDate = new Date();
                        objDate.setTime(mSeconds);
                        // compare input parameter date and created Date() object
                        // if difference exists then date isn't valid
                        if (objDate.getFullYear() != year)  return false;
                        if (objDate.getMonth()    != month) return false;
                        if (objDate.getDate()     != day)   return false;
                        // otherwise return true
                        return true;

                   }
           function mayor (val,title) {
                var vday   = val.substring(0,2)  - 0;
                var vmonth = val.substring(3,5)  - 1; // because months in JS start with 0
                var vyear  = val.substring(6,10) - 0;
                var tday   = title.substring(0,2)  - 0;
                var tmonth = title.substring(3,5)  - 1; // because months in JS start with 0
                var tyear  = title.substring(6,10) - 0;
                vmSeconds = (new Date(vyear, vmonth, vday)).getTime();
                tmSeconds = (new Date(tyear, tmonth, tday)).getTime();
                // initialize Date() object from calculated milliseconds
                if(vmSeconds<=tmSeconds){
                    return true;
                }else {
                    return false;
                }
           }
           var defaults = {type:'text' , regex : /^([A-Za-zñÑáéíóúÁÉÍÓÚ ]+)$/, accordion: '' , index: 0 , maxi : 0 , mini: 0};
           var regextext = /^([A-Za-zñÑáéíóúÁÉÍÓÚ ]+)$/;
           var regexnum = /^\d+$/;
           var regexdecimal = /^[-+]?[0-9]+(\.[0-9]+)?$/;
           var regexemail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
           var regexaddress = /^([A-Za-zñÑáéíóúÁÉÍÓÚ .0-9\#\)\(\'\-\/\_\,\°\-\:\;]+)$/;
           var regexdate = /^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/;
           var regexhour = /^(\d{1,2})[./:](\d{1,2})$/;
           var regexdateNormal = /^(\d{1,2})[.//](\d{1,2})[.//](\d{4})$/;
           var settings = $.extend(defaults,options);
           switch (settings.type) {
                case 'text':
                    if ( !(regextext.test($(this).val()))) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
						$(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'none':
                    if ( $(this).val()=='' || $(this).val()=='NULL' ) {
                        return true;
                    } else {
                        return false;
                    }
                    break;
                case 'date':
                    if ( !( regexdate.test($(this).val()) && isDate($(this).val()) )) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'datenew':
                    if ( !( regexdate.test($(this).val()) && isDate($(this).val()) && mayor($(this).val(),$(this).attr('title')) )) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'hour':
                    if ( !( regexhour.test($(this).val()) && $(this).val().length==5 )) {
                        $(this).addClass('ui-state-error');
                        $(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                 case 'dateNormal':
                    if ( !( regexdateNormal.test($(this).val()) && isDateNormal($(this).val()) && mayor($(this).val(),$(this).attr('title')) )) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        //$(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                   case 'dateNormalSin':
                    if ( !( regexdateNormal.test($(this).val()) && isDateNormal($(this).val()) )) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        //$(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'email':
                    if ( !( regexemail.test( $(this).val()))) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'address':
                    //$(this).val($(this).val().replace(/^\s+/g,'').replace(/\s+$/g,''));
                    if (!(regexaddress.test($(this).val()))) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'number':
                    if ( !( regexnum.test( $(this).val()))) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;   
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;
                case 'length':
                    if ( $(this).val().length > settings.maxi || $(this).val().length < settings.mini ) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;   
                    }else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                    break;                                        
                case 'num':
                    if ( !( regexnum.test( $(this).val()))) {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;   
                    }else {
                        if ($(this).val()!=0) {
                            $(this).removeClass('ui-state-error');
                            return true;
                        }else {
                            $(this).addClass('ui-state-error');
                            return false;
                        }
                    }
                    break;
                    case 'decimal':
                	if (!( regexdecimal.test( $(this).val()))) {
				        $(this).addClass('ui-state-error');
                       //$(settings.accordion).accordion( "activate" , settings.index );
                       $(this).focus();
				           return false;
			        } else {
                        if ($(this).val()!=0) {
                            $(this).removeClass('ui-state-error');
                            return true;
                        }else {
                            $(this).addClass('ui-state-error');
                            return false;
                        }
			        }
                     break;
                    case 'decimal2':
                	if (!( regexdecimal.test( $(this).val()))) {
				        $(this).addClass('ui-state-error');
                       $(this).focus();
				           return false;
			        } else {
                            $(this).removeClass('ui-state-error');
                            return true;
                        }
                     break;
                case 'select':
                    if ($(this).val()=="NULL") {
                        $(this).addClass('ui-state-error');
                        //$(settings.accordion).accordion( "activate" , settings.index );
                        $(this).focus();
                        return false;
                    } else {
                        $(this).removeClass('ui-state-error');
                        return true;
                    }
                     break;
                default:
                    break;
            }
       }
    });
})(jQuery);

